package com.numberone.master.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.modules.sys.entity.Complain;
import com.numberone.master.modules.sys.entity.User;
import com.numberone.master.modules.sys.service.ComplainService;
import com.numberone.master.modules.sys.utils.UserUtils;



@Controller
@RequestMapping(value = "${adminPath}/sys/complain")
public class ComplainController extends BaseController{
	@Autowired
	private ComplainService complainService;
	
	@ModelAttribute("complain")
	public Complain get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return complainService.get(id);
		}else{
			return new Complain();
		}
	}
	
	@RequestMapping(value = {""})
	public String index(Complain complain, Model model) {
//        model.addAttribute("list", officeService.findAll());
		return "modules/sys/complainIndex";
	}
	
	@RequestMapping(value = "list")
	public String list(Complain complain, Model model) {
        model.addAttribute("list", complainService.findAll());
		return "modules/sys/complainList";
	}
	
	@RequestMapping(value = "form")
	public String form(Complain complain, Model model){
		User user = UserUtils.getUser();
		if (complain.getParent()==null || complain.getParent().getId()==null){
			complain.setParent(user.getComplain());
		}
		complain.setParent(complainService.get(complain.getParent().getId()));
		// 自动获取排序号
		if (StringUtils.isBlank(complain.getId())&&complain.getParent()!=null){
			int size = 0;
			List<Complain> list = complainService.findAll();
			for (int i=0; i<list.size(); i++){
				Complain e = list.get(i);
				if (e.getParent()!=null && e.getParent().getId()!=null
						&& e.getParent().getId().equals(complain.getParent().getId())){
					size++;
				}
			}
			complain.setCode(complain.getParent().getCode() + StringUtils.leftPad(String.valueOf(size > 0 ? size+1 : 1), 3, "0"));
		}
		model.addAttribute("complain", complain);
		return "modules/sys/complainForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Complain complain, Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, complain)){
			return form(complain, model);
		}
		complainService.save(complain);
		addMessage(redirectAttributes, "保存投诉类型'" + complain.getName() + "'成功");
		String id = "0".equals(complain.getParentId()) ? "" : complain.getParentId();
		return "redirect:" + adminPath + "/sys/complain/list?id="+id+"&parentIds="+complain.getParentIds();
	}
	
	@RequiresPermissions("sys:complain:edit")
	@RequestMapping(value = "delete")
	public String delete(Complain complain, RedirectAttributes redirectAttributes) {
		
//		if (Area.isRoot(id)){
//			addMessage(redirectAttributes, "删除区域失败, 不允许删除顶级区域或编号为空");
//		}else{
		complainService.delete(complain);
			addMessage(redirectAttributes, "删除区域成功");
//		}
		return "redirect:" + adminPath + "/sys/complain/list?id="+complain.getParentId()+"&parentIds="+complain.getParentIds();
	}
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Complain> list = complainService.findAll();
		for (int i=0; i<list.size(); i++){
			Complain e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("pIds", e.getParentIds());
				map.put("name", e.getName());
			
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	

}
